﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StudyEnglish.Model;
using System.Windows.Media;

namespace StudyEnglish
{
    public partial class VolSearch : PhoneApplicationPage
    {
        public VolSearch()
        {
            InitializeComponent();
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
        }
        private TextBlock SearchVisualTree(DependencyObject targetElement, string controlName)
        {
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return null;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is TextBlock)
                {
                    TextBlock targetItem = (TextBlock)child;

                    if (targetItem.Name == controlName)
                    {
                        return targetItem;
                    }
                }
                else
                {
                    SearchVisualTree(child, controlName);
                }
            }
            return null;
        }

        private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(obj);
            if (count == 0)
                return null;
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        private void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if (lstVolSearch.Items.Count > 0)
            {
                for (int i = 0; i < lstVolSearch.Items.Count; i++)
                {
                    var temp = lstVolSearch.ItemContainerGenerator.ContainerFromIndex(i);
                    ListBoxItem myListBoxItem = temp as ListBoxItem;
                    if (myListBoxItem != null)
                    {
                        StackPanel tb = FindVisualChild<StackPanel>(myListBoxItem);
                        TextBlock tb1 = SearchVisualTree(tb, "tb_pronoun");
                        TextBlock tb2 = SearchVisualTree(tb, "tb_Meaning");

                        if ((e.Orientation == PageOrientation.LandscapeRight) || (e.Orientation == PageOrientation.LandscapeLeft))
                        {
                            tb1.Width = 775;
                            tb2.Width = 775;
                        }
                        else if ((e.Orientation == PageOrientation.PortraitDown) || (e.Orientation == PageOrientation.PortraitUp))
                        {
                            tb1.Width = 433;
                            tb2.Width = 433;
                        }
                    }
                }
            }
        }

        private void SearchProcess()
        {
            App.VolVM.GetVolContainsWord(txt_searchWord.Text);
            if (App.VolVM.LstVolContainsWord.Count == 0)
            {
                lbl_empty.Visibility = Visibility.Visible;
                lstVolSearch.ItemsSource = null;
            }
            else
            {
                lbl_empty.Visibility = Visibility.Collapsed;
                lstVolSearch.ItemsSource = App.VolVM.LstVolContainsWord;
            }
            //lstVolSearch.Focus();
        }

        private void btn_search_Click(object sender, RoutedEventArgs e)
        {
            SearchProcess();
        }

        private void Grid_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var element = sender as FrameworkElement;
            AppVocabulary item;
            if (element != null)
            {
                item = element.DataContext as AppVocabulary;
                NavigationService.Navigate(new Uri("/VolDetail.xaml?id=" + item.ID, UriKind.Relative));
            }
        }

        private void txt_searchWord_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //var isEnterKey = e.Key == System.Windows.Input.Key.Enter || e.PlatformKeyCode == 10;

            //if (isEnterKey)
            //{
                SearchProcess();
            //}
        }

        //private void logout_Click(object sender, RoutedEventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/UserLogin.xaml", UriKind.Relative));
        //}

        private void txt_searchWord_TextChanged_1(object sender, TextChangedEventArgs e)
        {

        }
    }
}